package com.zoweunion.mechanic.util;

import org.dom4j.DocumentHelper;

import java.util.*;

/**
 * @author xp
 * @date 2020年07月10日2:59 PM
 */
public class XMLUtil {
    /***
              *XML格式字符串转换成MAP,不包括List
              * @param xml
              * @return
              */
    public static Map xmlToMap(String xml) {
        try {
            Map map = new HashMap();
            org.dom4j.Document document = DocumentHelper.parseText(xml);
            org.dom4j.Element nodeElement = document.getRootElement();
            List node = nodeElement.elements();
            for (Iterator it = node.iterator(); it.hasNext();) {
                org.dom4j.Element elm = (org.dom4j.Element) it.next();
                map.put(elm.getName(), elm.getText());
                elm = null;
            }
            node = null;
            nodeElement = null;
            document = null;
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /***
              * XML格式字符串转换成list
              * @param xml
              * @return
              */
    public static List xmlToList(String xml) {
        try {
            List<Map> list = new ArrayList<Map>();
            org.dom4j.Document document = DocumentHelper.parseText(xml);
            org.dom4j.Element nodesElement = document.getRootElement();
            List nodes = nodesElement.elements();
            for (Iterator its = nodes.iterator(); its.hasNext();) {
                org.dom4j.Element nodeElement = (org.dom4j.Element) its.next();
                Map map = xmlToMap(nodeElement.asXML());
                list.add(map);
                map = null;
            }
            nodes = null;
            nodesElement = null;
            document = null;
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /***
              * XML格式字符串转换成map,包括List
              * @param xml
              * @return
              */
    public static Map xmlToListMap(String xml) {
        try {
            Map reMap = new HashMap();
            org.dom4j.Document document = DocumentHelper.parseText(xml);
            org.dom4j.Element nodesElement = document.getRootElement();
            List nodes = nodesElement.elements();
            for (Iterator its = nodes.iterator(); its.hasNext();) {
                org.dom4j.Element nodeElement = (org.dom4j.Element) its.next();
                if(nodeElement.isTextOnly()){
                    reMap.put(nodeElement.getName(), nodeElement.getText());
                }else{
                    List<Map> reList = xmlToList(nodeElement.asXML());
                    reMap.put(nodeElement.getName(),reList);
                    reList = null;
                }
            }
            nodes = null;
            nodesElement = null;
            document = null;
            return reMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
              * list的element转xml格式String
              * @param list
              * @param element
              * @return
              */
    public static org.dom4j.Element list2xml(List list,org.dom4j.Element element){
        int i = 0;
        for (Object o : list) {
            org.dom4j.Element nodeElement = element.addElement("Map");
            if (o instanceof Map) {
                //nodeElement.addAttribute("type", "o");
                Map m = (Map) o;
                for (Iterator iterator = m.entrySet().iterator(); iterator.hasNext();) {
                    Map.Entry entry = (Map.Entry) iterator.next();
                    org.dom4j.Element keyElement = nodeElement.addElement(entry.getKey().toString());
                    if (entry.getValue() instanceof List) {
                        //keyElement.addAttribute("type", "l");
                        list2xml((List) entry.getValue(),keyElement);
                    } else {
                        //keyElement.addAttribute("type", "s");
                        keyElement.setText(entry.getValue().toString());
                    }
                }
            } else if (o instanceof List) {
                //nodeElement.addAttribute("type", "l");
                list2xml((List)o,nodeElement);
            }
            else {
                org.dom4j.Element keyElement = nodeElement.addElement("value");
                keyElement.addAttribute("num", String.valueOf(i));
                keyElement.setText(String.valueOf(o));
            }
            i++;
        }
        return element;
    }



}
